import * as menuService from '@/services/menu';
import { message } from 'antd';
import { filter } from '@/utils/utils';
import { DEFAULT_RESPONE_SUCCESS } from '@/constants';

export default {
  namespace: 'menu',

  state: {
    current: 1,
    total: 0,
    data: [],
    rowKey: 'id'
  },

  subscriptions: {

  },

  effects: {
    *queryData({ payload }, { call, put }) {
      const response = yield call(menuService.queryMenuTreeList, payload);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = [] } = response;

        if (payload.value) {
          yield put({
            type: 'filter', payload: {
              data: result,
              ...payload
            }
          });
        } else {
          yield put({
            type: 'changeState',
            payload: {
              data: result,
              current: payload.current,
              pageSize: payload.pageSize,
              total: Array.isArray(result) ? result.length : 0
            }
          });
        }
      }
    },
    *filter({ payload }, { call, put }) {
      const { value, data, current, pageSize } = payload;

      if (value) {
        const newData = filter(data, value, ['code', 'name']);
        yield put({
          type: 'changeState',
          payload: {
            current,
            filterData: newData,
            filterValue: value,
            pageSize,
            total: newData.length,
          }
        });
      } else {
        yield put({
          type: 'changeState',
          payload: {
            current,
            filterData: [],
            filterValue: '',
            pageSize,
            total: data.length,
          }
        });
      }
    },
    *saveOrUpdateData({ payload }, { call, put }) {
      const { data, ...extra } = payload;
      const option = {
        method: 'POST',
        body: { ...data, parentId: data.parentId || 0 }
      };
      let response = {};
      if (!data.id) {
        response = yield call(menuService.saveMenu, option);
      } else {
        response = yield call(menuService.updateMenu, option);
      }
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success('操作成功', 1);
        yield put({
          type: 'changeState',
          payload: { visible: false, editData: {} }
        });
        yield put({ type: 'queryData', payload: { ...extra } });
      }
    },
    *deleteData({ payload }, { call, put }) {
      const { id, ...extra } = payload;

      const response = yield call(menuService.deleteMenu, { id });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success('操作成功', 1);
        yield put({ type: 'queryData', payload: { ...extra } });
      }
    }
  },

  reducers: {
    clear(state, { payload }) {
      return {
        current: 1,
        total: 0,
        data: [],
        rowKey: 'id'
      };
    },
    changeState(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    }
  },
};
